package org.lxy.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.api.R;
import org.lxy.domain.GoodsTypeEntity;
import org.lxy.domain.RecycleGoodsEntity;
import org.lxy.domain.constant.CoreConstant;
import org.lxy.service.GoodsTypeService;
import org.lxy.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/goodstype")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam Map<String, Object> params){
        List<GoodsTypeEntity> list = goodsTypeService.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("changepwd");
        return modelAndView;
    }

    @RequestMapping("operation")
    public ModelAndView operation(Long id, String op, String type){
        ModelAndView modelAndView = new ModelAndView();
        if (op.equals(null)) {

        } else if (CoreConstant.LIST.equals(op)) {
            List<GoodsTypeEntity> list = goodsTypeService.list();
            modelAndView.addObject("list", list);
            modelAndView.setViewName("/wupinleixing/wupinleixinglist");
        } else if (CoreConstant.ADD.equals(op)) {
            GoodsTypeEntity entity = new GoodsTypeEntity();
            modelAndView.addObject("entitys", entity);
            modelAndView.setViewName("/wupinleixing/wupinleixingEdit");
        } else if (CoreConstant.EDIT.equals(op)) {
            GoodsTypeEntity entity = goodsTypeService.getById(id);
            modelAndView.addObject("entitys", entity);
            modelAndView.setViewName("/wupinleixing/wupinleixingEdit");
        }
        return modelAndView;
    }

    @RequestMapping("edit")
    @ResponseBody
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        GoodsTypeEntity entity = new GoodsTypeEntity();
        entity.setId(Integer.parseInt(request.getParameter("id")));entity.setName(request.getParameter("leixingmingcheng"));entity.setNote(request.getParameter("beizhu"));
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        boolean tag = false;
        if (entity.getId() > 0) {
            tag = goodsTypeService.updateById(entity);
        } else {
            tag = goodsTypeService.save(entity);
        }
        if (tag) {

            PrintWriter out = response.getWriter();
            out.write(
                    "<script>alert('操作成功!');window.parent.location.href='/goodstype/operation?op=List" + "';</script>");

        } else {
            PrintWriter out = response.getWriter();
            out.write("<script>alert('操作失败!');</script>");
            List<GoodsTypeEntity> list = goodsTypeService.list();
            request.setAttribute("plist", list);
            request.setAttribute("entitys", entity);
            request.getRequestDispatcher("/wupinleixing/wupinleixingEdit").forward(request, response);

        }
    }


    @RequestMapping("delete")
    @ResponseBody
    public Integer delete(String ids){
        return goodsTypeService.removeByIds(Arrays.asList(ids.split(","))) ? 1 : 0;
    }

/*
    *//**
     * 信息
     *//*
    @RequestMapping("/info/{id}")
    public ModelAndView info(@PathVariable("id") Integer id){
		GoodsTypeEntity goodsType = goodsTypeService.getById(id);

        return R.ok().put("goodsType", goodsType);
    }

    *//**
     * 保存
     *//*
    @RequestMapping("/save")
    public ModelAndView save(@RequestBody GoodsTypeEntity goodsType){
		goodsTypeService.save(goodsType);

        return R.ok();
    }

    *//**
     * 修改
     *//*
    @RequestMapping("/update")
    public ModelAndView update(@RequestBody GoodsTypeEntity goodsType){
		goodsTypeService.updateById(goodsType);

        return R.ok();
    }

    *//**
     * 删除
     *//*
    @RequestMapping("/delete")
    public ModelAndView delete(@RequestBody Integer[] ids){
		goodsTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }*/

}
